package com.zy.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zy.common.pojo.EUDataGridResult;
import com.zy.common.util.HttpClientUtil;
import com.zy.common.util.TaotaoResult;
import com.zy.mapper.TbContentMapper;
import com.zy.pojo.TbContent;
import com.zy.pojo.TbContentExample;
import com.zy.pojo.TbContentExample.Criteria;
import com.zy.service.ContentService;

@Service
public class ContentServiceImpl implements ContentService {

	@Autowired
	private TbContentMapper tbContentMapper;
	
	@Value("${REST_BASE_URL}")
	private String REST_BASE_URL;
	@Value("${REST_CONTENT_SYNC_URL}")
	private String REST_CONTENT_SYNC_URL;
	/**
	 * 分页查询
	 */
	@Override
	public EUDataGridResult getContentList(int page, int rows, long categoryId) {
		// 创建模板对象
		TbContentExample tbContentExample = new TbContentExample();
		Criteria createCriteria = tbContentExample.createCriteria();
		createCriteria.andCategoryIdEqualTo(categoryId);
		// 分页
		PageHelper.startPage(page, rows);
		List<TbContent> list = tbContentMapper.selectByExample(tbContentExample);
		PageInfo<TbContent> pageInfo = new PageInfo<>(list);
		// 总条数
		long total = pageInfo.getTotal();
		EUDataGridResult euDataGridResult = new EUDataGridResult();
		euDataGridResult.setTotal(total);
		euDataGridResult.setRows(list);
		return euDataGridResult;
	}

	/**
	 * 更新
	 */
	@Override
	public TaotaoResult updateContent(TbContent tbContent) {
		TbContentExample example = new TbContentExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(tbContent.getId());
		tbContent.setUpdated(new Date());
		tbContentMapper.updateByExample(tbContent, example);
		//添加缓存同步了逻辑,使得前台首页大广告及时更新，因为在不同的服务器中，所以需要通过rpc远程调用，http请求到tao_rest中拦截执行清除缓存功能
		try {
			//http://localhost:8081/rest/cache/sync/content/content.getCategoryId()
			HttpClientUtil.doGet(REST_BASE_URL+REST_CONTENT_SYNC_URL+tbContent.getCategoryId());
		}catch (Exception e) {
			e.printStackTrace();
		}
		return TaotaoResult.ok();
	}

	/**
	 * 添加
	 */
	@Override
	public TaotaoResult insertContent(TbContent content) {
		// 补全pojo内容
		content.setCreated(new Date());
		content.setUpdated(new Date());
		tbContentMapper.insert(content);
		//添加缓存同步了逻辑,使得前台首页大广告及时更新，因为在不同的服务器中，所以需要通过rpc远程调用，http请求到tao_rest中拦截执行清除缓存功能
		try {
			//http://localhost:8081/rest/cache/sync/content/content.getCategoryId()
			HttpClientUtil.doGet(REST_BASE_URL+REST_CONTENT_SYNC_URL+content.getCategoryId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return TaotaoResult.ok();
	}

	/**
	 * 删除
	 */
	@Override
	public TaotaoResult deleteContent(long categoryId) {
		TbContent content = tbContentMapper.selectByPrimaryKey(categoryId);
		tbContentMapper.deleteByPrimaryKey(categoryId);
		//添加缓存同步了逻辑,使得前台首页大广告及时更新，因为在不同的服务器中，所以需要通过rpc远程调用，http请求到tao_rest中拦截执行清除缓存功能
		try {
			//http://localhost:8081/rest/cache/sync/content/content.getCategoryId()
			HttpClientUtil.doGet(REST_BASE_URL+REST_CONTENT_SYNC_URL+content.getCategoryId());
		}catch (Exception e) {
			e.printStackTrace();
		}
		return TaotaoResult.ok();
	}

}
